package com.shigu.searchWord.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.opentae.data.mall.beans.KeywordView;
import com.opentae.data.mall.examples.KeywordViewExample;
import com.opentae.data.mall.interfaces.KeywordViewMapper;
import com.shigu.PageResponse;
import com.shigu.exceptions.AdminException;
import com.shigu.searchWord.api.KeywordsService;
import com.shigu.searchWord.vo.IndexHotSearchWordList;
import com.shigu.searchWord.vo.KeywordResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service(value = "keywordsServiceImpl")
public class KeywordsServiceImpl implements KeywordsService{

    @Autowired
    KeywordViewMapper keywordViewMapper;

    @Override
    public PageResponse<KeywordResponseVo> getHotWordList() {
        KeywordViewExample example = new KeywordViewExample();
        example.setOrderByClause("weight desc");
        List<KeywordView> keywordViews = keywordViewMapper.selectByExample(example);
        PageResponse<KeywordResponseVo> response = new PageResponse<>();
        response.setData(keywordViews.stream().map(o->{
            KeywordResponseVo vo = new KeywordResponseVo();
            vo.setText(o.getText());
            vo.setColorVal(o.getColorVal());
            return vo;
        }).collect(Collectors.toList()));
        return response;
    }

    @Override
    public void setOrUpdateHotWord(Long hotWordId, String hotKeyword, String weight, String colorVal) throws AdminException {
        if(StringUtils.isBlank(hotKeyword)){
            throw new AdminException("热搜关键词不能为空");
        }
        if(StringUtils.isBlank(weight)){
            throw new AdminException("权重不能为空");
        }
        if(StringUtils.isBlank(colorVal)){
            throw new AdminException("颜色值不能为空");
        }
        KeywordView keywordView = new KeywordView();
        keywordView.setText(hotKeyword);
        keywordView.setColorVal(colorVal);
        keywordView.setWeight(Integer.valueOf(weight));
        if(hotWordId == null){
            keywordViewMapper.insertSelective(keywordView);
        }else{
            keywordView.setKeywordId(hotWordId);
            keywordViewMapper.updateByPrimaryKeySelective(keywordView);
        }
    }

    @Override
    public PageResponse<IndexHotSearchWordList> getIndexHotSearchWord() {
        KeywordViewExample example = new KeywordViewExample();
        example.setOrderByClause("weight desc");
        List<KeywordView> keywordViews = keywordViewMapper.selectByExample(example);
        PageResponse<IndexHotSearchWordList> response = new PageResponse<>();
        response.setData(keywordViews.stream().map(o->{
            IndexHotSearchWordList keywordView = new IndexHotSearchWordList();
            keywordView.setHotWordId(o.getKeywordId());
            keywordView.setWeight(o.getWeight());
            keywordView.setHotKeyword(o.getText());
            keywordView.setColorVal(o.getColorVal());
            return keywordView;
        }).collect(Collectors.toList()));
        return response;
    }

    @Override
    public void deleteIndexHotSearchWord(Long hotWordId) throws AdminException {
        if(hotWordId == null){
            throw new AdminException("参数错误");
        }
        keywordViewMapper.deleteByPrimaryKey(hotWordId);
    }
}
